﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using PowerToolsWeb.DataAccess;
using PowerToolsWeb.Facade;

namespace PowerToolsWeb.Admin
{
    public partial class AdminList : AdminPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindAdmin();
        }
        void BindAdmin()
        {
            Tpt_AdminCollection admin = new Tpt_AdminCollection();
            admin.ListAll();
            this.GridView1.DataSource = admin.DataTable;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            if (commandName == "changePWD")
            {
                Response.Redirect("AddAdmin.aspx?adminid=" + e.CommandArgument);
            }
            else if (commandName == "deleteAdmin")
            {
                Tpt_Admin admin = new Tpt_Admin();
                if(!admin.Delete(int.Parse(e.CommandArgument.ToString())))
                {
                    Alert(Page,"删除失败");
                    return;
                }
                BindAdmin();
            }
        }

        public Boolean IsSAAdmin
        {
            get
            {
                if (UserInfo.UserName == "admin")
                    return true;
                else
                    return false;
            }
        }

        protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                if (IsSAAdmin || DataBinder.Eval(e.Row.DataItem, "loginname").ToString() == UserInfo.UserName)
                    ((HtmlGenericControl)e.Row.FindControl("divControl")).Visible = true;
                else
                    ((HtmlGenericControl)e.Row.FindControl("divControl")).Visible = false;
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E8E6EA'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
}
